package com.code.sslsocket;

import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;

/**
 * @description: 安全的websocket
 * @className: HTTPSClient
 * @package: com.code.sslsocket
 * @author: yangzhi@hsyuntai.com
 * @date: 2019/10/17 18:52
 * @copyright: 版权归 HSYUNTAI 所有
 */
public class HTTPSClient {

    public static void main(String[] args) {

        int port = 443;
        String host = "cn.bing.com";

        SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
        SSLSocket socket = null;
        try {

            socket =(SSLSocket) factory.createSocket(host, port);

            String[] supported = socket.getSupportedCipherSuites();
            socket.setEnabledCipherSuites(supported);

            Writer out = new OutputStreamWriter(socket.getOutputStream(), "UTF-8");
            out.write("GET http://" + host+ "/ HTTP/1.1\r\n");
            out.write("Host: "+ host + "\r\n");
            out.write("\r\n");
            out.flush();


            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            String s;
            while (!(s = in.readLine()).equals("")) {
                System.out.println(s);
            }
            System.out.println();

            String contentLength = in.readLine();
            int length = Integer.MAX_VALUE;
            try {
                length = Integer.parseInt(contentLength.trim(), 16);
            } catch (Exception e){

            }
            System.out.println(contentLength);

            int c;
            int i = 0;
            while ((c = in.read()) != -1 && i++ < length) {
                System.out.println(c);
            }
            System.out.println();

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (socket != null) {
                    socket.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
